package com.controller;

import com.pojo.R;
import com.pojo.Teacher;
import com.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @GetMapping("/allTeacher")
    public R list() {
        List<Teacher> list = teacherService.queryAllTeacher();
        return R.ok(list);
    }

    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody Teacher teacher) {
        teacherService.addTeacher(teacher);
        return R.ok();
    }

    @PostMapping("/del/{id}")
    public R deleteTeacher(@PathVariable("id") Long id) {
        teacherService.deleteTeacherById(id);
        return R.ok();
    }

    @PostMapping("/update")
    public R updateTeahcer(@RequestBody Teacher teacher) {
        teacherService.updateTeacher(teacher);
        teacher = teacherService.queryById(teacher.getTeacherId());
        return R.ok(teacher);
    }

    @GetMapping("queryById")
    public R queryById(@RequestParam("id") Long id) {
        Teacher teacher = teacherService.queryById(id);
        return R.ok(teacher);
    }
}
